﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynaPrecision.FastFourierTransform
{
	public class ChebyshevDerivativeFastApproach
	{
		public static void Differentiate<TList, T>(IList<T> from, ref TList to)
			where TList : IList<T>
		{
			while (to.Count < from.Count)
			{
				to.Add(DefaultValueProvider<T>.Zero);
			}
			to[from.Count - 1] = DynaPrecision.DefaultValueProvider<T>.Zero;
			to[from.Count - 2] = (dynamic)from[from.Count - 1] * 2 * (from.Count - 1);
			for (int i = from.Count - 3; i >= 0; i--)
			{
				to[i] = (dynamic)from[i + 1] * 2 * (i + 1) + to[i + 2];
			}
			to[0] = (dynamic)to[0] / 2;
		}
		public static void Differentiate<T>(IList<T> from)
		{
			int N = from.Count;
			T u_i_plus_1 = from[N-1], u_i;
			from[N - 1] = DynaPrecision.DefaultValueProvider<T>.Zero;
			u_i = from[N-2];
			from[N - 2] = (dynamic)u_i_plus_1 * 2 * (N - 1);
			for (int i = N - 3; i >= 0; i--)
			{
				u_i_plus_1 = u_i;
				u_i = from[i];
				from[i] = (dynamic)u_i_plus_1 * 2 * (i + 1) + from[i + 2];
			}
			from[0] = (dynamic)from[0] / 2;
		}
	}
}
